package github.fafaovo.common.item;

import github.fafaovo.common.Itemgroup.ExampleGroup;
import github.fafaovo.common.config.exampleconfig;
import github.fafaovo.common.mycapability.ISpeedUpCapability;
import github.fafaovo.core.registry.CommonSetupEventHandler;
import github.fafaovo.network.NetworkRegistryHandler;
import github.fafaovo.network.Packet.Power;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Food;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.fml.network.PacketDistributor;

public class examplefood extends Item {
    private static final Food food = (new Food.Builder())
            .saturation(0) //饱腹度
            .hunger(0) //鸡腿
            .setAlwaysEdible() //满饱腹度也能吃
            .fastToEat() //吃的更快
            .build();


    public examplefood() {
        //super(new Properties().group(ExampleGroup.exampleGroup).food(food));
        super(new Properties().group(ExampleGroup.exampleGroup).food(food));
    }

    @Override
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, LivingEntity entityLiving) {
        if(!worldIn.isRemote() && entityLiving instanceof PlayerEntity)
        {
            int amount = 100;
            LazyOptional<ISpeedUpCapability> capability = entityLiving.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
            capability.ifPresent((c)->{
                float examplePower = c.getLevel();
                if(!exampleconfig.example_boolean.get())
                {
                    c.setLevel(examplePower + amount);
                }
                else
                {
                    if(examplePower - amount <= 0)
                        c.setLevel(0);
                    else
                        c.setLevel(examplePower - amount);
                }
                NetworkRegistryHandler.CHANNEL.send(
                        PacketDistributor.PLAYER.with(
                                ()->(ServerPlayerEntity) entityLiving),//为了区分不同的玩家
                        new Power(c.getLevel())
                );
                //NetworkRegistryHandler.CHANNEL.sendToServer(new PowerClinetForServer(c.getLevel()));
            });
        }

        return super.onItemUseFinish(stack, worldIn, entityLiving);
    }
}
